package com.tang.hawkmusic.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProvider;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Window;
import android.widget.FrameLayout;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.gson.Gson;
import com.tang.hawkmusic.R;
import com.tang.hawkmusic.activity.local.LocalActivity;
import com.tang.hawkmusic.bean.UserInfo;
import com.tang.hawkmusic.bean.music.LocalMusic;
import com.tang.hawkmusic.utill.ThemeUtill;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class MainActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        //设置沉浸式状态栏
        setStatusBar();
        //初始化Viewpager2和bottomNavigationView
        initViewPager();
        getGranted();
        mainViewModel=new ViewModelProvider(this).get(MainViewModel.class);
        mainViewModel.setFragments(getFragments());
        mainViewModel.setActivity(this);
        
    }

    /*resultCode 登录是1 ，本地播放是2*/
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode){
            case 1: //登录返回数据
                loginResult(data);
                break;
            case 2: //本地音乐播放返回数据
                localMusicResult(data);
                break;
        }


    }

    private void localMusicResult(Intent data) {
        ArrayList<LocalMusic> musics = null;
        int position=0;
        try {
            musics= (ArrayList<LocalMusic>) data.getSerializableExtra("musics");
            position=data.getIntExtra("position",0);
        }catch (Exception ignored){

        }
        //viewmodel保存数据
        mainViewModel.setLocalMusics(musics);
        mainViewModel.setCurrentItem(position);
        playerFragment.setLocal(true);
        setPlayBegin();
    }

    public void loginResult(Intent data){
        UserInfo userInfo=null;
        try {
            assert data != null;
            userInfo= (UserInfo) data.getSerializableExtra("userInfo");
        }catch (Exception ignored){

        }
        if (userInfo!=null&&userInfo.getCode()==200){
            mainViewModel.loginSuccess(userInfo);
            setUserInfo(userInfo);
            mainViewModel.getPlayList();
            //updatePlayList();
        }
    }

    public void getGranted(){
        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
        }
    }

}